package com.pomr.controller.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pomr.service.sys.PersonRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.form.AgencyUp;
import com.pomr.model.form.PersonRecordUp;
import com.pomr.model.sys.Agency;
import com.pomr.model.sys.City;
import com.pomr.model.sys.Coutry;
import com.pomr.model.sys.Province;
import com.pomr.service.common.CommonService;
import com.pomr.util.common.CommonData;
import com.pomr.util.common.ResponseData;

@Controller
@RequestMapping("/common")
public class CommonController {
	@Autowired
	@Qualifier("commonServiceImpl")
	private CommonService commonService;

	@Autowired
	@Qualifier("personRecordServiceImpl")
	private PersonRecordService personRecordService;
	
	@RequestMapping(value="/inter-selectcity",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData findCitys(@RequestBody Province province){
		ResponseData pd = null;
		try {
			List<City> citys = new ArrayList<City>();
			List<City> cl = (List<City>)CommonData.data.get("City");
			System.out.println(cl.size());
			int len = cl.size();
			City c = null;
			for(int i=0; i<len; i++){
				c = cl.get(i);
				if(c.getProvince().getId().equals(province.getId())){
					citys.add(c);
				}
			}
			pd = ResponseData.ok();
			pd.putDataValue("City", citys);
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-selectcoutry",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData findCoutrys(@RequestBody City city){
		ResponseData pd = null;
		System.out.println(city.getId());
		try {
			List<Coutry> coutrys = new ArrayList<Coutry>();
			List<Coutry> cl = (List<Coutry>)CommonData.data.get("Coutry");
			int len = cl.size();
			Coutry c = null;
			for(int i=0; i<len; i++){
				c = cl.get(i);
				if(c.getCity().getId().equals(city.getId())){
					coutrys.add(c);
				}
			}
			pd = ResponseData.ok();
			pd.putDataValue("Coutry", coutrys);
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 通过身份证号或者姓名查询患者领用的设备附带的SIM卡
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-findSim",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData findSimByIdCard(@RequestBody Map<String, String> map) throws Exception{
		ResponseData pd = null;
		String cont = map.get("cont");
		if(cont==null || cont.equals("")){
			pd = ResponseData.custom("查询条件不能为空");
		}else{
			List sims = commonService.findSimByIdCard(cont);
			pd = ResponseData.ok();
			pd.putDataValue("sims", sims);
		}
		return pd;
	}
	
	/**
	 * 获取字典信息
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-searchmenu",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchMenus(@RequestBody Map<String, String> map) throws Exception{
		ResponseData pd = null;
		String keys = map.get("keys");
		String[] keyarray = keys.split(",");
		pd = ResponseData.ok();
		for(String key:keyarray){
			pd.putDataValue(key, CommonData.data.get(key));
		}
		return pd;
	}
	
	/**
	 * 上传健康档案
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loadPersonRecords")
	@ResponseBody
	public ResponseData loadPersonRecords(@RequestBody List<PersonRecordUp> prus){
		ResponseData pd = null;
		try{
			if(commonService.loadPersonRecords(prus)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.custom("保存失败，确认上传信息是否准确");
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		
		return pd;
		
	}
	
	@RequestMapping(value="/loadAgency")
	@ResponseBody
	public ResponseData loadAgency(@RequestBody AgencyUp agu){
		ResponseData pd = null;
		try{
			int key  = commonService.loadAgency(agu);
			if(key > 0){
				pd = ResponseData.ok();
				Agency ag = commonService.findAgency(key);
				pd.putDataValue("agency", ag);
			}else{
				pd = ResponseData.custom("机构信息上传失败");
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		
		return pd;
		
	}

	@RequestMapping(value="/app-searchFriendTel",method=RequestMethod.GET)
	@ResponseBody
	public ResponseData searchFriendTel(String idcard) throws Exception{
		return personRecordService.searchFriendTelApp(idcard);
	}

}
